package com.yndsoft.jc.supplier.order.entry;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.EntryDetailAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.OrderDetailModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.widget.recyclerview.ScrollLinearLayoutManager;

import java.util.List;

public class EntryDetailActivity extends SupplierBaseActivity implements View.OnClickListener {

    private TextView orderDetailTime;
    private TextView orderDetailNumber;
    private RecyclerView orderDetailResultList;
    private String id;

    @Override
    public int getContentResId() {
        return R.layout.activity_entry_detail;
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, EntryDetailActivity.class);
        intent.putExtra(Constants.ID, id);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        orderDetailTime = findViewById(R.id.order_detail_time);
        orderDetailNumber = findViewById(R.id.order_detail_number);
        orderDetailResultList = findViewById(R.id.order_detail_result_list);
        barTitle.setText("订单详情");
        barBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        id = getIntent().getStringExtra(Constants.ID);
        mHttpUtil.supplierOrderDetail(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1:
                        String msg = returnMsg.getInfo();
                        if (msg != null) {
                            setData(msg);
                        }
                        break;
                }
            }
        }, "order", mSession.getSid(), id);
    }

    private void setData(String msg) {
        OrderDetailModel model = new Gson().fromJson(msg, OrderDetailModel.class);
        orderDetailTime.setText(model.getCreatetime() != null ? model.getCreatetime() : "");
        orderDetailNumber.setText("订单号: " + model.getId());
        List<OrderDetailModel.DetsBean> dets = model.getDets();
        if (dets != null && dets.size() > 0) {
            mCache.put(Constants.SIZE, dets.size() + "");
            orderDetailResultList.setLayoutManager(new ScrollLinearLayoutManager(mContext));
            orderDetailResultList.setAdapter(new EntryDetailAdapter(mContext, dets, id));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
        }
    }
}